package com.apes.framework.plugin.enumeration.parse;

import com.apes.framework.plugin.enumeration.EnumType;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.JpaUtil;
import com.apes.framework.util.ReflectUtil;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.enumeration.model.EnumerationType;
import com.apes.scm.masterdata.enumeration.service.EnumerationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.PropertyDescriptor;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 功能：comment 服务
 *
 * @author xul
 * @create 2018-11-02 16:14
 */
@Service
@Order
public class EnumGenerator implements CommandLineRunner {

    @Value("${spring.jpa.generate-enum:true}")
    private boolean generateEnum;

    @Autowired
    private EnumerationService enumerationService;

    /**
     * 监听Model遍历事件
     *
     * @param request
     */
    @Transactional(rollbackFor = Exception.class)
    public void onModelRead(SimpleRequest request) {
        if (!generateEnum) return;
        try {
            // todo 未完成
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run(String... args) throws Exception {
//        JpaUtil.getAllDomainType().forEach(domainTypeName -> {
//            Class domainType = ReflectUtil.getClass(domainTypeName);
//            onModelRead(domainType);
//        });
    }

    @Transactional(rollbackFor = Exception.class)
    public void onModelRead(Class entityClazz) {

        // 测试使用
        if ( entityClazz.getName().indexOf("TestEnumModel") > 0) {
            System.out.println(entityClazz.getName());
        }

        List<PropertyDescriptor> descriptors = Arrays.asList(ReflectUtil.getPropertyDescriptors(entityClazz));
        descriptors.stream()
                .filter(descriptor -> ReflectUtil.getAnnotation(entityClazz, descriptor.getName(), EnumType.class) != null)
                .forEach(descriptor -> {
                    EnumType enumType = ReflectUtil.getAnnotation(entityClazz, descriptor.getName(), EnumType.class);
                    if (enumType != null) {
                        EnumerationType enumerationType = new EnumerationType();
                        enumerationType.setId(enumType.id());
                        enumerationType.setDescription(enumType.name());
                        enumerationType.setSysInner(enumType.sys_inner());
                        Set<Enumeration> enumerations = new HashSet();
                        Arrays.stream(enumType.values()).forEach( enumValue -> {
                            Enumeration enumeration = new Enumeration();
                            enumeration.setId(enumValue.id());
                            enumeration.setCode(enumValue.code());
                            enumeration.setDescription(enumValue.name());
                            enumeration.setType(enumerationType);
                            enumerations.add(enumeration);
                        });
                        enumerationType.setEnumerations(enumerations);
                        enumerationService.annotationSave(enumerationType);
                    }
                });
    }

}
